﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.ENT;

namespace KM.DAT
{
    public class datKhuyenMai
    {
        public datKhuyenMai()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        public void insert(entKhuyenMai entKM)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiInsert", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[15];
            sqla[0] = new SqlParameter("TieuDe", DbType.Int32);
            sqla[1] = new SqlParameter("GiaBan", DbType.Int32);
            sqla[2] = new SqlParameter("GiaKM", DbType.Int32);
            sqla[3] = new SqlParameter("ThoiGian", DbType.Int32);
            sqla[4] = new SqlParameter("Link", DbType.Int32);
            sqla[5] = new SqlParameter("ImageSmall", DbType.Int32);
            sqla[6] = new SqlParameter("SiteNguon", DbType.Int32);
            sqla[7] = new SqlParameter("Provider", DbType.Int32);
            sqla[8] = new SqlParameter("Summary", DbType.Int32);
            sqla[9] = new SqlParameter("LoaiKhuyenMaiId", DbType.Int32);
            sqla[10] = new SqlParameter("TrangThai", DbType.Int32);
            sqla[11] = new SqlParameter("NguoiTao", DbType.Int32);
            sqla[12] = new SqlParameter("NgayTao", DbType.Int32);
            sqla[13] = new SqlParameter("NguoiSua", DbType.Int32);
            sqla[14] = new SqlParameter("NgaySua", DbType.Int32);

            sqla[0].Value = entKM.TieuDe;
            sqla[1].Value = entKM.GiaBan;
            sqla[2].Value = entKM.GiaKM;
            sqla[3].Value = entKM.ThoiGian;
            sqla[4].Value = entKM.Link;
            sqla[5].Value = entKM.ImageSmall;
            sqla[6].Value = entKM.SiteNguon;
            sqla[7].Value = entKM.Provider;
            sqla[8].Value = entKM.Summary;
            sqla[9].Value = entKM.LoaiKhuyenMaiId;
            sqla[10].Value = entKM.TrangThai;
            sqla[11].Value = entKM.NguoiTao;
            sqla[12].Value = entKM.NgayTao;
            sqla[13].Value = entKM.NguoiSua;
            sqla[14].Value = entKM.NgaySua;


            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        public List<entKhuyenMai> getAll(int loaiKhuyenMaiId)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetAllByLoaiKhuyenMaiId", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("LoaiKhuyenMaiId", loaiKhuyenMaiId);

            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            while (dr.Read())
            {
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                entKM.GiaBan = dr["GiaBan"].ToString();
                entKM.GiaKM = dr["GiaKM"].ToString();
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entKM);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        public entKhuyenMai getInfo(entKhuyenMai entKMTS)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("KhuyenMaiId", DbType.Int32);
            sqla[0].Value = entKMTS.KhuyenMaiId;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            if (dr.Read())
            {
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TitleDeal"].ToString();
                entKM.GiaBan = dr["GiaNiemYet"].ToString();
                entKM.GiaKM = dr["GiaKM"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.LoaiKhuyenMaiId = Convert.ToInt32(dr["LoaiKhuyenMaiId"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua =Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

            }
            dr.Close();
            sqlConnection.Close();
            return entKM;
        }
        public void edit(entKhuyenMai entKM)
        {
        }
    }
}